// 安装  npm  i  @reduxjs/toolkit
// 导入  createSlice 创建切片   configureStore 配置仓库
import {createSlice} from '@reduxjs/toolkit';

// 创建切片对象
let countSlice = createSlice({
  //状态的名称
  name: 'count',
  //状态的初始值
  initialState: 500,
  //设置 reducers 属性, 配置 reducer 函数的功能
  reducers: {
    //新增
    incre: (state, action) => {
      return state + action.payload;
    },
    //减少
    decre: (state, action) => {
      return state - action.payload;
    }
  }
});

// 获取同步的 action creator
export let {incre, decre} = countSlice.actions;

// 获取 reducer 函数
let countReducer = countSlice.reducer;

//暴露 reducer 函数
export default countReducer;

//声明异步的 action creator
export let asyncIncreAction = (payload) => {
  return dispatch => {
    setTimeout(() => {
      //更新状态
      dispatch(incre(payload));
    }, 1000)
  }
}